﻿using System.Collections.Generic;
using HMIS.FrameWork;
using HMIS.FrameWork.Cache;
using HMIS.FrameWork.DomainBase;
using HMIS.FrameWork.Repository.Nhb;
using HMIS.Infrastructure.Core.Authority;

namespace HMIS.Infrastructure.Core.Repositories.Impl
{
    public class ActionPermissionRepositoryImpl : RepositoryNhbImpl<ActionPermission>, IActionPermissionRepository
    {
        private readonly ICache Cache;
        public ActionPermissionRepositoryImpl(ICache Cache)
        {
            this.Cache = Cache;
        }
        public override void SaveOrUpdate(ActionPermission entity)
        {
            //注意这里重写基类的方法，内聚了数据库验证逻辑
            if (IsFieldExist("ActionName", entity.ActionName, entity.Id, entity.ControllerName))
                throw new ExistException("ActionName");

            base.SaveOrUpdate(entity);
        }

        #region IActionPermissionRepository 成员

        public IList<ActionPermission> GetPlistAll(int start, int limit, string sort, string dir, List<DataFilter> filters, out long total)
        {
            var strFilter = base.GetHqlstrByExtFilter(filters, "t");
            sort = "t." + sort;

            var query = Session.CreateQuery(@"select t from ActionPermission as t"
                    + (string.IsNullOrEmpty(strFilter) ? string.Empty : " where " + strFilter)
                    + " order by " + sort + " " + dir)
                    .SetFirstResult(start)
                    .SetMaxResults(limit);

            total = Session.CreateQuery(@"select count(*) from ActionPermission as t"
                 + (string.IsNullOrEmpty(strFilter) ? string.Empty : " where " + strFilter))
                .UniqueResult<long>();

            return query.List<ActionPermission>();
        }

        public bool IsFieldExist(string fieldName, string fieldValue, string id)
        {
            return base.IsFieldExist(fieldName, fieldValue, id, null);
        }

        public new bool IsFieldExist(string fieldName, string fieldValue, string id, string controllerName)
        {
            return base.IsFieldExist(fieldName, fieldValue, id, string.Format(@"o.ControllerName='{0}'", controllerName));
        }

        public IList<ActionPermission> GetListByUserId(string userId)
        {
            var query = Session.CreateQuery(@"select distinct t 
                                                from ActionPermission as t
                                                left join t.Roles as r
                                                left join r.Persons p
                                               where p.Id = :UserId and r.IsDelete = 0 and p.IsDelete = 0 and p.State = :PersonState and r.State=:RoleState")
                 .SetString("UserId", userId)
                 .SetEnum("PersonState", PersonState.Normal)
                 .SetEnum("RoleState", RoleState.Normal);
            return query.List<ActionPermission>();
        }

        public IList<ActionPermission> GetListByRoleId(string roleId)
        {
            var query = Session.CreateQuery(@"select distinct t 
                                                from ActionPermission as t
                                                left join t.Roles as r
                                               where r.Id = :RoleId and r.IsDelete = 0 and r.State = :RoleState")
                 .SetString("RoleId", roleId)
                 .SetEnum("RoleState", RoleState.Normal);
            return query.List<ActionPermission>();
        }
        public IList<ActionPermission> GetAllActionPermissions()
        {
            if (Cache.GetApplicationCache("actionpermission") == null)
            {
                Cache.SetApplicationCache("actionpermission", base.GetAll());
            }
            return (IList<ActionPermission>)Cache.GetApplicationCache("actionpermission");
        }
        #endregion
    }
}
